/*
 * StringPrototype.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.interpreter.Evaluator;
import org.scriptme.ecmascript.interpreter.ScopeChain;

/**
 * The Class StringPrototype.
 */
class StringPrototype extends ESObject {

	/** The Constant LENGTHstring. */
	private static final String LENGTHstring = ("length").intern();

	/** The Constant LENGTHhash. */
	private static final int LENGTHhash = LENGTHstring.hashCode();

	/** The value. */
	ESString value = new ESString("");

	/**
	 * Instantiates a new string prototype.
	 * 
	 * @param prototype
	 *            the prototype
	 * @param evaluator
	 *            the evaluator
	 */
	StringPrototype(ESObject prototype, Evaluator evaluator) {
		super(prototype, evaluator);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getESClassName()
	 */
	public String getESClassName() {
		return "String";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#toString()
	 */
	public String toString() {
		return value.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESString()
	 */
	public ESValue toESString() {
		return value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#booleanValue()
	 */
	public boolean booleanValue() throws EcmaScriptException {
		return value.booleanValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#doubleValue()
	 */
	public double doubleValue() throws EcmaScriptException {
		return value.doubleValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getPropertyInScope(java.lang.String,
	 *      org.scriptme.ecmascript.interpreter.ScopeChain, int)
	 */
	public ESValue getPropertyInScope(String propertyName,
			ScopeChain previousScope, int hash) throws EcmaScriptException {
		if (hash == LENGTHhash && propertyName.equals(LENGTHstring)) {
			return new ESNumber(value.getStringLength());
		}
		return super.getPropertyInScope(propertyName, previousScope, hash);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getProperty(java.lang.String,
	 *      int)
	 */
	public ESValue getProperty(String propertyName, int hash)
			throws EcmaScriptException {
		if (hash == LENGTHhash && propertyName.equals(LENGTHstring)) {
			return new ESNumber(value.getStringLength());
		} else {
			return super.getProperty(propertyName, hash);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getSpecialPropertyNames()
	 */
	public String[] getSpecialPropertyNames() {
		String[] ns = { LENGTHstring };
		return ns;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#toJavaObject()
	 */
	public Object toJavaObject() {
		return value.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#toDetailString()
	 */
	public String toDetailString() {
		return "ES:[Object: builtin " + this.getClass().getName() + ":"
				+ ((value == null) ? "null" : value.toString()) + "]";
	}

	/**
	 * Information routine to check if a value is a string if true, must
	 * implement toString without a evaluator.
	 * 
	 * @return true
	 */
	public boolean isStringValue() {
		return true;
	}
}
